package com.zhouyu;

import com.zhouyu.service.ItemService;
import com.zhouyu.service.OrderService;
import com.zhouyu.service.ProductService;
import org.springframework.beans.factory.config.RuntimeBeanNameReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.format.Printer;

public class ConstructorTest {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.register(AppConfig8.class);
		//1.指定有两个OrderService的构造方法来进行bean的创建
//		ItemService itemService = (ItemService) context.getBean("itemService", new OrderService(), new OrderService());
		//2.通过定义BeanDefinition来指定构造方法入参值
//		AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
//		beanDefinition.setBeanClass(ProductService.class);
//		beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(new OrderService());
//		beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(new OrderService());
//		context.registerBeanDefinition("productService",beanDefinition);
//		context.refresh();
//		ProductService itemService = (ProductService) context.getBean("productService");

		//3.程序员想让Spring自动选择构造方法以及构造方法的入参值， autowire="constructor"
//		AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
//		beanDefinition.setBeanClass(ProductService.class);
		//supplier机制
//		beanDefinition.setInstanceSupplier(ProductService::new);
//		beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(1,new OrderService());
//		beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanNameReference("userService"));
//		beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(new OrderService());
//		beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
//		context.registerBeanDefinition("productService",beanDefinition);
		context.refresh();
		ProductService itemService = (ProductService) context.getBean("productService");
	}
}
